package net.lab1024.sa.admin.module.business.exam.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.admin.module.business.exam.dao.PaperDao;
import net.lab1024.sa.admin.module.business.exam.dao.PaperaDao;
import net.lab1024.sa.admin.module.business.exam.domain.entity.PaperEntity;
import net.lab1024.sa.admin.module.business.exam.domain.entity.PaperaEntity;
import net.lab1024.sa.admin.module.business.exam.domain.form.add.PaperAddForm;
import net.lab1024.sa.admin.module.business.exam.domain.form.query.PaperQueryForm;
import net.lab1024.sa.admin.module.business.exam.domain.form.update.PaperUpdateForm;
import net.lab1024.sa.admin.module.business.exam.domain.vo.PaperVO;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 课程分类 Service
 *
 * @Author AKITA
 * @Date 2025-06-06 11:28:07
 * @Copyright AKITA
 */

@Service
public class PaperService {

    @Resource
    private PaperDao paperDao;
    @Resource
    private PaperaDao paperaDao;

    public PageResult<PaperVO> queryPage(PaperQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<PaperVO> list = paperDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    public ResponseDTO<Long> add(PaperAddForm addForm) {
        PaperEntity paperEntity = SmartBeanUtil.copy(addForm, PaperEntity.class);
        paperDao.insert(paperEntity);
        for (PaperaEntity paperaEntity : addForm.getLista()) {
            paperaEntity.setDocEntry(paperEntity.getDocEntry());
            paperaDao.insert(paperaEntity);
        }
        return ResponseDTO.ok(paperEntity.getDocEntry());
    }

    @Transactional
    public ResponseDTO<String> update(PaperUpdateForm updateForm) {
        PaperEntity paperEntity = SmartBeanUtil.copy(updateForm, PaperEntity.class);
        if (paperDao.updateById(paperEntity)== 0) {
            throw new RuntimeException("数据已被更新，刷新后重试！");
        }
        paperaDao.deleteById(updateForm.getDocEntry());
        for (PaperaEntity paperaEntity : updateForm.getLista()) {
            paperaDao.insert(paperaEntity);
        }



        return ResponseDTO.ok();
    }

    public ResponseDTO<String> batchDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return ResponseDTO.ok();
        }

        paperDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    @Transactional
    public ResponseDTO<String> delete(Long docEntry) {
        paperDao.deleteById(docEntry);
        paperaDao.deleteById(docEntry);
        return ResponseDTO.ok();
    }

    public ResponseDTO<PaperEntity> info(Long docEntry) {
        PaperEntity paperEntity = paperDao.selectById(docEntry);
        paperEntity.setLista(paperaDao.selectList(new LambdaQueryWrapper<PaperaEntity>().eq(true, PaperaEntity::getDocEntry, docEntry)));
        return ResponseDTO.ok(paperEntity);
    }


    @Transactional
    public ResponseDTO<PaperEntity> comTestPaperOk(Long docEntry) {


        return ResponseDTO.ok();
        
    }
}
